package com.sen.pro.switches.core.cfg;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.sen.common.util.PropertiesUtils;

/**
 * properties配置信息读取类
 * 
 * @author tian qingsen
 */
public final class ConfigReader {
	
	private static Map<String, ConfigReader> configReaders = new HashMap<String, ConfigReader>();

	private Properties prop = null;

	private ConfigReader(String url) throws IOException {
		prop = PropertiesUtils.loadProperties(url);
	}

	/**
	 * 获取配置信息读取类实体
	 * @param url 配置文件地址
	 * @return
	 * @throws IOException
	 */
	public static ConfigReader getInstance(String url) throws IOException {
		ConfigReader configReader = configReaders.get(url);
		if (configReader == null) {
			configReader = new ConfigReader(url);
			configReaders.put(url, configReader);
		}
		return configReader;
	}

	/**
	 * 根据Key获取对应的内容值
	 * @param key
	 * @return 内容值，如果未找到Key，则返回""
	 * @throws Exception
	 */
	public String getContent(String key) throws Exception {
		return prop == null ? StringUtils.EMPTY : prop.getProperty(key);
	}
}
